package com.sk.weichat.ui.message.search;

import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.TextView;

import com.sk.weichat.R;
import com.sk.weichat.adapter.SearchImageVideoContntContentAdapter;
import com.sk.weichat.bean.message.ChatMessage;
import com.sk.weichat.bean.message.ChatMessageEntity;
import com.sk.weichat.bean.message.XmppMessage;
import com.sk.weichat.db.dao.ChatMessageDao;
import com.sk.weichat.ui.base.BaseActivity;
import com.sk.weichat.util.DateUtils;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

/**
 * 查找指定内容
 * 图片与视频
 */
public class SearchImageVideoContent extends BaseActivity {

    public static int TYPE_IMAGE = 0;
    public static int TYPE_VIDEO = 1;
    private int mSearchType;
    private String mSearchObject;
    private RecyclerView mRecyclerView;
    private SearchImageVideoContntContentAdapter mSearchImageVideoContntContentAdapter;
    private List<ChatMessageEntity>mChatMessageEntity = new ArrayList<>();
    private TextView tvEmpty;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_search_image_video_content);
        mSearchType = getIntent().getIntExtra("search_type", TYPE_IMAGE);
        mSearchObject = getIntent().getStringExtra("search_objectId");

        if (mSearchType == TYPE_IMAGE) {
            // 全局变量，用于滑动展示
            List<ChatMessage> imageList = ChatMessageDao.getInstance().queryChatMessageByType(coreManager.getSelf().getUserId(), mSearchObject, XmppMessage.TYPE_IMAGE);
            // 根据timeSend排序
            Comparator<ChatMessage> comparator = (o1, o2) -> (int) (o2.getDoubleTimeSend() - o1.getDoubleTimeSend());
            Collections.sort(imageList, comparator);
            Map<String, List<ChatMessage>> mapData = new HashMap();
            for (int i = 0; i < imageList.size(); i++) {
                String time = DateUtils.getYearMonthString(imageList.get(i).getTimeSend());
                List<ChatMessage> data = mapData.get(time);
                //进行判空，如果当前的map没有添加当前key，新建一个List保存数据
                if (data == null) {
                    data = new ArrayList();
                    data.add(imageList.get(i));
                    mapData.put(time, data);
                } else {
                    //已经添加，就往原List数据中添加新的数据
                    data.add(imageList.get(i));
                }
            }
            Iterator it = mapData.keySet().iterator();
            while (it.hasNext()) {
                String key = it.next().toString();
                ChatMessageEntity chatMessageEntity = new ChatMessageEntity();
                chatMessageEntity.setDateEntity(key);
                chatMessageEntity.setmChatMessage(mapData.get(key));
                mChatMessageEntity.add(chatMessageEntity);
            }
        } else {
            List<ChatMessage> videoList = ChatMessageDao.getInstance().queryChatMessageByType(coreManager.getSelf().getUserId(), mSearchObject, XmppMessage.TYPE_VIDEO);
            // 根据timeSend排序
            Comparator<ChatMessage> comparator = (o1, o2) -> (int) (o2.getDoubleTimeSend() - o1.getDoubleTimeSend());
            Collections.sort(videoList, comparator);
            Map<String, List<ChatMessage>> mapData = new HashMap();
            for (int i = 0; i < videoList.size(); i++) {
                String time = DateUtils.getYearMonthString(videoList.get(i).getTimeSend());
                List<ChatMessage> data = mapData.get(time);
                //进行判空，如果当前的map没有添加当前key，新建一个List保存数据
                if (data == null) {
                    data = new ArrayList();
                    data.add(videoList.get(i));
                    mapData.put(time, data);
                } else {
                    //已经添加，就往原List数据中添加新的数据
                    data.add(videoList.get(i));
                }
            }
            Iterator it = mapData.keySet().iterator();
            while (it.hasNext()) {
                String key = it.next().toString();
                ChatMessageEntity chatMessageEntity = new ChatMessageEntity();
                chatMessageEntity.setDateEntity(key);
                chatMessageEntity.setmChatMessage(mapData.get(key));
                mChatMessageEntity.add(chatMessageEntity);
            }
        }

        initActionBar();
        initView();
    }


    private void initActionBar() {
        getSupportActionBar().hide();
        findViewById(R.id.iv_title_left).setOnClickListener(v -> finish());
        TextView tvTitle = findViewById(R.id.tv_title_center);
        if (mSearchType == TYPE_IMAGE) {
            tvTitle.setText(getString(R.string.s_image));
        } else {
            tvTitle.setText(getString(R.string.s_video));
        }
    }

    private void initView() {
        mRecyclerView = findViewById(R.id.s_image_video_rcy);
        mRecyclerView.setLayoutManager(new LinearLayoutManager(this));
        mSearchImageVideoContntContentAdapter = new SearchImageVideoContntContentAdapter(this,mChatMessageEntity);
        mRecyclerView.setAdapter(mSearchImageVideoContntContentAdapter);
        tvEmpty = findViewById(R.id.tv_record_empty);
        if (mChatMessageEntity.size()>0){
            mRecyclerView.setVisibility(View.VISIBLE);
            tvEmpty.setVisibility(View.GONE);
        }else {
            mRecyclerView.setVisibility(View.GONE);
            tvEmpty.setVisibility(View.VISIBLE);
            if (mSearchType == TYPE_IMAGE) {
                tvEmpty.setText("暂无图片");
            } else {
                tvEmpty.setText("暂无视频");
            }
        }
    }
}
